package com.seventh.parkingrent.controller;


import com.seventh.parkingrent.commens.Result;
import com.seventh.parkingrent.controller.form.ParkingSpaceAuditForm;
import com.seventh.parkingrent.controller.form.ParkingSpaceAuditFormForFront;
import com.seventh.parkingrent.domain.ParkingSpace;
import com.seventh.parkingrent.exception.CustomException;
import com.seventh.parkingrent.service.IParkingSpaceAuditService;
import com.seventh.parkingrent.service.IParkingSpaceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.text.ParseException;
import java.util.List;

/**
 * <p>
 * 停车场审核记录表 前端控制器
 * </p>
 *
 * @author Lucas
 * @since 2021-08-25
 */
@RestController
@CrossOrigin("*")
@Slf4j
public class ParkingSpaceAuditController {
    @Resource
    private IParkingSpaceAuditService parkingSpaceAuditService;

    @Resource
    private IParkingSpaceService parkingSpaceService;

    @GetMapping("/getParkingSpaceAuditByCondition")
    public Result getParkingSpaceAuditByCondition(String managerName,
                                                  String username,
                                                  String propertyName,
                                                  String parkingSpaceAddr,
                                                  String beginTime,
                                                  String endTime) throws ParseException {
        log.info("前端发来停车场审核查询数据managerName={},username={},propertyName={},parkingSpaceAddr={},beginTime={},endTime={}",managerName,username,propertyName,parkingSpaceAddr,beginTime,endTime);
        if(managerName==""){
            managerName=null;
        }
        if(username==""){
            username=null;
        }
        if(propertyName==""){
            propertyName=null;
        }
        if(parkingSpaceAddr==""){
            parkingSpaceAddr=null;
        }
        List<ParkingSpaceAuditFormForFront> parkingSpaces = parkingSpaceAuditService.getParkingSpaceAuditByCondition(managerName, username, propertyName, parkingSpaceAddr, beginTime, endTime);
        return Result.success(parkingSpaces);
    }

    @PostMapping("/confirmAuditParkingSpace")
    @Transactional
    public Result confirmAuditParkingSpace(@Valid @RequestBody ParkingSpaceAuditForm form,BindingResult bindingResult){
        log.info("获取前端停车场审核form={}",form);

        if (bindingResult.hasErrors()) {
            return getBindingResult(bindingResult);
        }


        //生成停车场审核记录
        Integer addParkingSpaceAuditResult = parkingSpaceAuditService.addParkingSpaceAuditResult(form);
        if(0==addParkingSpaceAuditResult){
            throw new CustomException("后端在添加审核记录表时发生错误");
        }

        //修改停车场中的状态和比例
        Integer updateResult = parkingSpaceService.updateParkingSpaceAudit(form);
        if(0==updateResult){
            throw new CustomException("后端在修改停车场表的审核结果时发生错误");
        }

        return Result.success();
    }


    public Result getBindingResult(BindingResult bindingResult){
        List<FieldError> fieldErrors = bindingResult.getFieldErrors();
        StringBuilder stringBuilder = new StringBuilder();
        for (FieldError fieldError : fieldErrors) {
            stringBuilder.append(fieldError.getField()+"："+fieldError.getDefaultMessage()+";");
        }
        return Result.fail("10086",stringBuilder.toString());
    }

}

